#!/usr/bin/env python
# encoding: utf-8
"""
toppage.py

Created by Manabu Terada on 2009-09-21.
Copyright (c) 2009 CMScom. All rights reserved.
"""
from Acquisition import aq_inner
from Products.CMFCore.utils import getToolByName
from zope.component import getMultiAdapter
from Products.CMFPlone.utils import getSiteEncoding
from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

from c2.site.icos09.config import TOPPEGE_CHAR_LIMIT

class TopPageView(BrowserView):
    template = ViewPageTemplateFile('toppage.pt')

    def __call__(self):
        return self.template()
    
    def sefe_title(self, title):
        s = title
        if not isinstance(s, unicode):
            s = s.decode(getSiteEncoding(self), 'replace')
        if len(s) > TOPPEGE_CHAR_LIMIT:
            s = s[:TOPPEGE_CHAR_LIMIT] + '...'
        return s
    
    def get_swf_tag(self):
        size_height = self.context.getFlash_h()
        size_width = self.context.getFlash_w()
        version = self.context.getFlash_v()
        uid = self.context.UID()
        swf_path = "%s/get_flash_file" % self.context.absolute_url()
        
        swf_tag_tmplate = """
              <div id="%s">
              <script type="text/javascript">
                var flashVars = {}
                var parms = {
                wmode :"transparent"
                }
                swfobject.embedSWF("%s", "%s", "%s", "%s", "%s","",flashVars,parms, {menu:"false"}, {})
              </script>
              </div>""" % (uid, swf_path, uid, size_width, size_height, version)
        return swf_tag_tmplate
    
    
    def get_news_items(self):
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')
        portal_state = getMultiAdapter((self.context, self.request), 
                                        name=u'plone_portal_state')
        portal = portal_state.portal()
        news_path = context.news_path
        if not news_path.startswith('/'):
            news_path = '/' + news_path
        path = '/'.join(portal.getPhysicalPath()) + news_path
        limit = context.news_count
        state = ['published']
        return catalog(portal_type='News Item',
                       review_state=state,
                       path=path,
                       sort_on='getObjPositionInParent',
                       sort_order='ascending',
                       sort_limit=limit)[:limit]
    
    def get_event_items(self):
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')
        portal_state = getMultiAdapter((self.context, self.request), 
                                        name=u'plone_portal_state')
        portal = portal_state.portal()
        event_path = context.event_path
        if not event_path.startswith('/'):
            event_path = '/' + event_path
        path = '/'.join(portal.getPhysicalPath()) + event_path
        limit = context.event_count
        state = ['published']
        return catalog(portal_type=['tfcaTrainingEvent', 'tfcaCall4Volunteer'],
                       review_state=state,
                       path=path,
                       sort_on='getObjPositionInParent',
                       sort_order='ascending',
                       sort_limit=limit)[:limit]
    
    def get_banner_items(self):
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')
        portal_state = getMultiAdapter((self.context, self.request), 
                                        name=u'plone_portal_state')
        portal = portal_state.portal()
        banner_path = context.banner_path
        if not banner_path.startswith('/'):
            banner_path = '/' + banner_path
        path = '/'.join(portal.getPhysicalPath()) + banner_path
        limit = context.banner_count
        # state = ['published']
        return catalog(portal_type='Banner',
                       # review_state=state,
                       path=path,
                       sort_on='getObjPositionInParent',
                       sort_order='ascending',
                       sort_limit=limit)[:limit]
    